package com.example.cityiot.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.android.volley.VolleyError;
import com.example.cityiot.MyApp;
import com.example.cityiot.R;
import com.example.cityiot.adapter.BusAdapter;
import com.example.cityiot.bean.BusBean;
import com.example.cityiot.utils.Request;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class FirstFragment extends Fragment {

    private MyApp myApp;
    private static String TAG = "FirstFragment";
    private ExpandableListView firstELV;
    private List<String> groupList;
    private List<List<BusBean>> childList;
    private BusAdapter busAdapter;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        myApp = (MyApp) getActivity().getApplication();
        init();

    }

    public void init(){
        firstELV = getActivity().findViewById(R.id.first_elv);

        groupList = new ArrayList<>();
        groupList.add("一号站台");
        groupList.add("二号站台");

        childList = new ArrayList<>();
        List<BusBean> childList1 = new ArrayList<>();
        childList1.add(new BusBean("一号公交车",1000));
        childList1.add(new BusBean("二号公交车",2000));
        childList.add(childList1);
        List<BusBean> childList2 = new ArrayList<>();
        childList2.add(new BusBean("一号公交车",1000));
        childList2.add(new BusBean("二号公交车",2000));
        childList.add(childList2);

        busAdapter = new BusAdapter(getActivity(),groupList,childList);
        firstELV.setAdapter(busAdapter);

        for(int i = 0; i < busAdapter.getGroupCount(); i++){
            firstELV.expandGroup(i);
        }

        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                getBusMsg(1);
                getBusMsg(2);
            }
        },0,3000);
    }

    public void getBusMsg(int busId){
        try {
            JSONObject body = new JSONObject();
            body.put("BusStationId",busId);
            body.put("UserName","admin");
            myApp.getRequest().post(
                    "/get_bus_station_info",
                    body,
                    new Request.OnRequestListener() {
                        @Override
                        public void OnSuccess(JSONObject msg) {
                            Log.i(TAG + "Success",msg.toString());
                            try {
                                JSONArray arr = msg.getJSONArray("ROWS_DETAIL");
                                JSONObject object1 = arr.getJSONObject(0);
                                int distance1 = object1.getInt("Distance");
                                JSONObject object2 = arr.getJSONObject(1);
                                int distance2 = object2.getInt("Distance");
                                if (busId == 1){
                                    childList.get(0).get(0).setDistance(distance1 > distance2 ? distance2/10 : distance1/10);
                                    childList.get(0).get(1).setDistance(distance1 > distance2 ? distance1/10 : distance2/10);
                                    childList.get(0).get(0).setName(distance1 > distance2 ? "二号公交车" : "一号公交车");
                                    childList.get(0).get(1).setName(distance1 > distance2 ? "一号公交车" : "二号公交车");
                                }else{
                                    childList.get(1).get(0).setDistance(distance1 > distance2 ? distance2/10 : distance1/10);
                                    childList.get(1).get(1).setDistance(distance1 > distance2 ? distance1/10 : distance2/10);
                                    childList.get(1).get(0).setName(distance1 > distance2 ? "二号公交车" : "一号公交车");
                                    childList.get(1).get(1).setName(distance1 > distance2 ? "一号公交车" : "二号公交车");
                                }
                                busAdapter.notifyDataSetChanged();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                        @Override
                        public void OnError(VolleyError volleyError) {
                            Log.e(TAG + "Error", volleyError.getMessage(),volleyError);
                        }
                    }
            );
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}